package com.goertek.easyface.device;

import com.goertek.easyface.entity.AAcsFaceDeviceConfig;
import com.goertek.easyface.service.AcsDataService;
import com.netsdk.lib.NetSDKLib;
import com.netsdk.module.GateModule;
import com.netsdk.module.LoginModule;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * @program: netsdk
 * @description: 测试人脸机
 * @author: devin.duj
 * @create: 2024-03-27 09:54
 */
@Slf4j
@Data
public class DeviceModal {
    private AAcsFaceDeviceConfig deviceInfo; //对应的设备信息
    private  String imageDir; //图片存储文件夹
    private AcsDataService acsDataService; //查询BPM流程记录
    private AnalyzerDataHander analyzerDataHander;

    private  NetSDKLib.LLong m_hLoginHandle = new NetSDKLib.LLong(0); // 登陆句柄
    private  NetSDKLib.LLong m_hAttachHandle = new NetSDKLib.LLong(0); // 订阅句柄

    public DeviceModal(AAcsFaceDeviceConfig deviceInfo, String imageDir, AcsDataService acsDataService){
        this.deviceInfo = deviceInfo;
        this.imageDir = imageDir;
        this.acsDataService = acsDataService;
        this.analyzerDataHander = new AnalyzerDataHander(deviceInfo,imageDir,acsDataService);
    }


    //开启智能订阅
    public boolean startSubscribe(){
        stopSubscribe();  //先退订，设备不会对重复订阅作校验，重复订阅后会有重复的事件返回
        m_hAttachHandle = GateModule.realLoadPic(deviceInfo.getSubscribeChannel(), analyzerDataHander ,m_hLoginHandle);
        if(m_hAttachHandle.longValue() != 0) {
            log.info("设备[{}] 订阅成功！订阅句柄：{}",deviceInfo.getDeviceName(),m_hAttachHandle.longValue());
            return true;
        } else {
            log.error("设备[{}] 订阅失败！订阅句柄：{}",deviceInfo.getDeviceName(),m_hAttachHandle.longValue());
            return false;
        }
    }
    //取消订阅
    public  void stopSubscribe(){
        GateModule.stopRealLoadPic(m_hAttachHandle);
    }

    // 登录
    public boolean login() {
        m_hLoginHandle = LoginModule.login(deviceInfo.getDeviceIp(),deviceInfo.getDevicePort(),deviceInfo.getDeviceUser(),deviceInfo.getDevicePass());
        if(m_hLoginHandle.longValue() != 0) {
            log.info("设备[{}] 登录成功! 登录句柄：{}",deviceInfo.getDeviceName(),m_hLoginHandle.longValue());
            return true;
        } else {
            log.error("设备[{}] 登录失败! 登录句柄：{}",deviceInfo.getDeviceName(),m_hLoginHandle.longValue());
            return false;
        }
    }

    // 登出
    public void logout() {
        GateModule.stopRealLoadPic(m_hAttachHandle);
        LoginModule.logout(m_hLoginHandle);
    }


}